<?php
/**
 * Created by PhpStorm.
 * User: 睿
 * Date: 2017-11-30
 * Time: 14:51
 * 优惠券类型：0普通优惠券  1门店关注券  2代金券  3会员权益优惠券（每月按等级发放）
 */

namespace app\store\controller;


use controller\BasicAdmin;
use service\DataService;
use think\Db;

class Coupon extends BasicAdmin
{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'StoreCoupon';

    /**
     * @Author  lingyun
     * @Desc    type 0普通优惠券
     * @return array|string
     */
    public function index()
    {
        $this->title = '普通优惠券列表';
        $get = $this->request->get();

        $store_id = session('user')['store_id'];

        $join = [
            ['ys_goods_category c', 'a.cat_id=c.id', 'LEFT'],
        ];

        $db = Db::name($this->table)->alias('a')->join($join)->order('a.status desc')->field('a.*,c.*,a.id as aid,a.status as status')->where('store_id', $store_id)->where('type', 0);
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }

        $qiniu = config('qiniu');
        $this->assign('qiniu', $qiniu['yangshi']);          //七牛样式后缀

        return parent::_list($db);
    }

    /**
     * 现金券
     * type  2  门店关注券
     * @return array|string
     */
    public function cash()
    {
        $this->title = '现金抵用券列表';
        $get = $this->request->get();

        $store_id = session('user')['store_id'];

        $join = [
            ['ys_goods_category c', 'a.cat_id=c.id', 'LEFT'],
        ];

        $db = Db::name($this->table)->alias('a')->join($join)->order('a.id desc')->field('a.*,c.*,a.id as aid,a.status as status')->where('store_id', $store_id)->where('type', 2);
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }

        $qiniu = config('qiniu');
        $this->assign('qiniu', $qiniu['yangshi']);          //七牛样式后缀

        return parent::_list($db);
    }

    /**
     * 关注优惠券
     * 1  门店关注券
     * @return array|string
     */
    public function follow()
    {
        $this->title = '关注优惠券';
        $get = $this->request->get();

        $store_id = session('user')['store_id'];

        $join = [
            ['ys_goods_category c', 'a.cat_id=c.id', 'LEFT'],
        ];

        $db = Db::name($this->table)->alias('a')->join($join)->order('a.status desc')->field('a.*,c.*,a.id as aid,a.status as status,a.is_recommend as recommend')->where('store_id', $store_id)->where('type', 1   );
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }

        $qiniu = config('qiniu');
        $this->assign('qiniu', $qiniu['yangshi']);          //七牛样式后缀

        return parent::_list($db);
    }

    /**
     * @Author  lingyun
     * @Desc    会员权益优惠券
     */
    public function qy_coupon(){
        $this->title = '权益优惠券';
        $get = $this->request->get();

        $store_id = session('user')['store_id'];

        $join = [
            ['ys_goods_category c', 'a.cat_id=c.id', 'LEFT'],
        ];

        $db = Db::name($this->table)->alias('a')->join($join)->order('a.status desc')->field('a.*,c.*,a.id as aid,a.status as status,a.is_recommend as recommend')->where('store_id', $store_id)->where('type', 3);
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }

        $qiniu = config('qiniu');
        $this->assign('qiniu', $qiniu['yangshi']);          //七牛样式后缀

        return parent::_list($db);
    }


    /**
     * 下架
     */
    public function forbid()
    {
        if (DataService::update($this->table)) {
            $this->success("优惠券下架成功！", '');
        }
        $this->error("优惠券下架失败，请稍候再试！");
    }

    /**
     * 上架
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("优惠券上架成功！", '');
        }
        $this->error("优惠券上架失败，请稍候再试！");
    }

    /**
     * 添加优惠券
     * @return array|string
     */

    public function add()
    {
        $store_id = session('user')['store_id'];
        if (request()->ispost()) {
            $send = explode(' - ', input('send'));
            $use = explode(' - ', input('use'));

            $post = input('');
            if (isset($post['status']))
                $status = 1;
            else
                $status = 0;

            $insert = db($this->table)->insert([
                'store_id' => $store_id,
                'name' => input('name'),
                'img' => input('img'),
                'cat_id' => input('cat_id'),
                'condition' => input('condition'),
                'create_num' => input('create_num'),
                'send_start_time' => strtotime($send[0]),
                'send_end_time' => strtotime($send[1]),
                'use_start_time' => strtotime($use[0]),
                'use_end_time' => strtotime($use[1]),
                'morethan' => input('morethan'),
                'reduce' => input('reduce'),
                'addtime' => time(),
                'status' => $status,
                'unit'=>'个'
            ]);
            if ($insert !== false)
                $this->success('优惠券添加成功！', '');
        } else {
            $cat = db('goods_category')->select();

            $this->assign('cat', $cat);

            return $this->_form($this->table, 'form');
        }
    }

    /**
     * 添加现金券
     * @return array|string
     */

    public function addc()
    {
        $store_id = session('user')['store_id'];
        if (request()->ispost()) {

            $post = input('');
            if (isset($post['status']))
                $status = 1;
            else
                $status = 0;
            $use = explode(' - ', input('use'));
            $insert = db($this->table)->insert([
                'store_id' => $store_id,
                'name' => input('name'),
                'img' => input('img'),
                'pc_beijing' => input('pc_beijing'),
                'm_beijing' => input('m_beijing'),
                'cat_id' => input('cat_id'),
                'create_num' => input('create_num'),
                'addtime' => time(),
                'status' => $status,
                'price' => input('price'),
                'value' => input('value'),
                'use_start_time' => strtotime($use[0]),
                'use_end_time' => strtotime($use[1]),
                'type' => 2,
                'unit'=>'个'
            ]);
            if ($insert !== false)
                $this->success('现金券添加成功！', '');
        } else {
            $cat = db('goods_category')->select();

            $this->assign('cat', $cat);

            return $this->_form($this->table, 'addc');
        }
    }

    /**
     * @Author  lingyun
     * @Desc    添加关注券
     */
    public function add_follow(){
        $store_id = session('user')['store_id'];
        if (request()->ispost()) {
            $post = input('');
            if (isset($post['status']))
                $status = 1;
            else
                $status = 0;
            $insert = db($this->table)->insert([
                'store_id'=>$store_id,
                'name' => input('name'),
                'img' => input('img'),
                'pc_beijing' => input('pc_beijing'),
                'm_beijing' => input('m_beijing'),
                'img' => input('img'),
                'cat_id' => input('cat_id'),
                'morethan' => input('morethan'),
                'reduce' => input('reduce'),
                'status' => $status,
                'addtime'=>time(),
                'type'=>1,
                'unit'=>'个'
            ]);
            if ($insert !== false)
                $this->success('优惠券添加成功！', '');
        } else {
            $cat = db('goods_category')->where('pid',0)->select();      //商品分类

            $level = db('sys_level')->order('score asc')->select();     //会员等级信息

            $this->assign('cat', $cat);
            $this->assign('level', $level);

            return $this->_form($this->table, 'fedit');
        }
    }

    public function edit()
    {
        if (request()->ispost()) {
            $send = explode(' - ', input('send'));
            $use = explode(' - ', input('use'));
            $post = input('');

            if (isset($post['status']))
                $status = 1;
            else
                $status = 0;
            $insert = db($this->table)->update([
                'id' => input('id'),
                'name' => input('name'),
                'img' => input('img'),
                'cat_id' => input('cat_id'),
                'condition' => input('condition'),
                'create_num' => input('create_num'),
                'send_start_time' => strtotime($send[0]),
                'send_end_time' => strtotime($send[1]),
                'use_start_time' => strtotime($use[0]),
                'use_end_time' => strtotime($use[1]),
                'morethan' => input('morethan'),
                'reduce' => input('reduce'),
                'status' => $status,
                'unit'=>'个'
            ]);
            if ($insert !== false)
                $this->success('优惠券修改成功！', '');
        } else {
            $cat = db('goods_category')->select();

            $this->assign('cat', $cat);

            return $this->_form($this->table, 'form');
        }
    }

    /**
     * 编辑现金券
     * @return array|string
     */
    public function editc()
    {
        if (request()->ispost()) {
            $post = input('');
            if(!empty($post['use'])){
                $use = explode(' - ', input('use'));
            }else{
                $use = explode(' - ', input('use'));
            }
            $send = explode(' - ', input('send'));

            if (isset($post['status']))
                $status = 1;
            else
                $status = 0;
            $insert = db($this->table)->update([
                'id' => input('id'),
                'name' => input('name'),
                'img' => input('img'),
                'pc_beijing' => input('pc_beijing'),
                'm_beijing' => input('m_beijing'),
                'cat_id' => input('cat_id'),
                'create_num' => input('create_num'),
                'addtime' => time(),
                'status' => $status,
                'use_start_time' => strtotime($use[0]),
                'use_end_time' => strtotime($use[1]),
                'price' => input('price'),
                'value' => input('value'),
                'unit'=>'个'
            ]);
            if ($insert !== false)
                $this->success('现金券修改成功！', '');
        } else {
            $cat = db('goods_category')->select();

            $this->assign('cat', $cat);

            return $this->_form($this->table, 'addc');
        }
    }

    /**
     * 编辑关注券
     * @return array|string
     */
    public function fedit()
    {
        if (request()->ispost()) {
            $post = input('');
            if(!empty($post['use'])){
                $use = explode(' - ', input('use'));
            }else{
                $use = explode(' - ', input('use'));
            }
            if (isset($post['status']))
                $status = 1;
            else
                $status = 0;
            $insert = db($this->table)->update([
                'id'=>input('id'),
                'name' => input('name'),
                'img' => input('img'),
                'pc_beijing' => input('pc_beijing'),
                'm_beijing' => input('m_beijing'),
                'cat_id' => input('cat_id'),
                'morethan' => input('morethan'),
                'reduce' => input('reduce'),
                'status' => $status,
                'use_start_time' => strtotime($use[0]),
                'use_end_time' => strtotime($use[1]),
                'unit'=>'个'
            ]);
            if ($insert !== false)
                $this->success('优惠券修改成功！', '');
        } else {
            $cat = db('goods_category')->select();

            $this->assign('cat', $cat);

            return $this->_form($this->table, 'fedit');
        }
    }

    /**
     * @Author  lingyun
     * @Desc    添加 -- 编辑会员权益优惠券
     */
    public function qyedit(){
        if (request()->ispost()) {
            $store_id = session('user')['store_id'];
            $post = input('post.');
            if($post['name'] == ''){
                $this->error('请输入优惠券名称','');
            }
            if($post['member_level_id'] == ''){
                $this->error('请选择发放会员类型','');
            }
            if($post['morethan'] == '' || $post['reduce'] == ''){
                $this->error('请输入使用规则','');
            }
            if($post['use'] == ''){
                $this->error('请选择期限','');
            }

            $post['type'] = 3;
            $use = explode(' - ', $post['use']);
            $post['store_id'] = $store_id;
            $post['unit'] = '个';
            if(isset($post['id'])){      //编辑
                unset($post['use']);
                $post['use_start_time'] = strtotime($use[0]);
                $post['use_end_time'] = strtotime($use[1]);
                $res = db('store_coupon')->update($post);
                $res!==false?$this->success('保存成功',''):$this->error('保存失败','');
            }else{      //添加
                unset($post['use']);
                $post['addtime'] = time();
                $post['use_start_time'] = strtotime($use[0]);
                $post['use_end_time'] = strtotime($use[1]);
                $res = db('store_coupon')->insert($post);
                $res>0?$this->success('保存成功',''):$this->error('保存失败','');
            }
        } else {
            $id = input('id');
            $vo = db('store_coupon')->where('id',$id)->find();
            $cat = db('goods_category')->select();
            $level = db('sys_level')->order('score asc')->select();

            $this->assign('cat', $cat);
            $this->assign('level', $level);
            $this->assign('vo', $vo);

            return $this->fetch('qyedit');
        }
    }

    /**
     * @Author  lingyun
     * @Desc    推荐
     */
    public function set_recommend(){
        $post = input('post.');
        $res = db('store_coupon')->where('id',$post['id'])->update([$post['field']=>$post['value']]);
        $res!==false?$this->success('设置成功',''):$this->error('设置失败','');
    }




}

